﻿Imports System.Data.SqlClient
Public Class FrmHoSocapgiayDangMuon
    Private Sub NapDonViHuyen()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from TENHUYEN", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        cmbhuyen.DisplayMember = "T_huyen"
        cmbhuyen.ValueMember = "maH"
        cmbhuyen.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub loadTenXa()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from TenXa where mahuyen = '" & cmbhuyen.SelectedValue.ToString & "' ", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbXa.DisplayMember = "TenXa"
        CmbXa.ValueMember = "maxa"
        CmbXa.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapTinhTrang()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMTinhTrang", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbTinhTrang.DisplayMember = "Tenluukho"
        CmbTinhTrang.ValueMember = "maTinhTrangID"
        CmbTinhTrang.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub XuatExcel()
        Dim gr As SaveFileDialog = New SaveFileDialog()
        Dim FileName As String
        With gr
            .InitialDirectory = "C:\"
            .Filter = "Microsoft Excel Worksheet (*.xls)|*.xls"
            .FilterIndex = 1
        End With
        If gr.ShowDialog = System.Windows.Forms.DialogResult.OK Then
            FileName = gr.FileName
        Else
            Exit Sub
        End If
        'Extracting from database
        Dim col, row As Integer
        Dim Excel As Object = CreateObject("Excel.Application")

        If Excel Is Nothing Then
            MsgBox("m***cel re..", MsgBoxStyle.Critical)
            Return
        End If
        'Export to Excel process
        Try
            With Excel
                .SheetsInNewWorkbook = 1
                .Workbooks.Add()
                .Worksheets(1).Select()
                .Worksheets(1).name = "Ho so muon"
                Dim i As Integer = 1
                For col = 0 To DataGridViewHoSoDangMuon.Columns.Count - 1
                    .cells(1, i).value = DataGridViewHoSoDangMuon.Columns(col).HeaderText
                    .cells(1, i).EntireRow.Font.Bold = True
                    i += 1
                Next
                i = 2
                Dim k As Integer = 1
                For col = 0 To DataGridViewHoSoDangMuon.Columns.Count - 1
                    i = 2
                    For row = 0 To DataGridViewHoSoDangMuon.Rows.Count - 1
                        With .worksheets(1)
                            .Cells(i, k).Value = DataGridViewHoSoDangMuon.Rows(row).Cells(col).Value
                            i += 1
                        End With
                    Next
                    k += 1
                Next
                .ActiveCell.Worksheet.SaveAs(FileName)
            End With
            System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)
            Excel = Nothing
            MsgBox("Dữ liệu đã được xuất sang định dạng Excel thành công!", MsgBoxStyle.Information)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        ' The excel is created and opened for insert value. We most close this excel using this system
        Dim pro() As Process = System.Diagnostics.Process.GetProcessesByName("EXCEL")
        For Each i As Process In pro
            i.Kill()
        Next
    End Sub
    Private Sub NapDataGridViewHoSoDangMuon()
        Dim TuNgay As String
        TuNgay = String.Format("{0:MM/dd/yyyy}", DateTuNgay.Value)
        Dim DenNgay As String
        DenNgay = String.Format("{0:MM/dd/yyyy}", DateDenNgay.Value)
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand("select  cg.sohoso,  ld.tenchu, ld.diachi, cg.nguoimuon, cg.donvi, cg.mucdichmuon,  cg.ngaymuon, cg.ngaytra " &
                               " From  CCTT_HOSOCAPGIAY cg, TENXA x, TENHUYEN h, TENCHULANDAU ld " &
                               " WHERE " &
                               " datediff (day, ngaymuon, '" & TuNgay & "') <= 0  " &
                               " and datediff (day, ngaymuon, '" & DenNgay & "') >= 0  " &
                               " and cg.xaid = '" & CmbXa.SelectedValue.ToString & "' " &
                               " and cg.huyenid ='" & cmbhuyen.SelectedValue.ToString & "' " &
                               " and cg.sohoso = ld.sohoso " &
                               " and cg.xaid = x.maxa " &
                               " and cg.huyenid = h.mah " &
                               " and cg.tinhtrangid = '" & CmbTinhTrang.SelectedValue.ToString & "' order by ngaymuon ", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        DataGridViewHoSoDangMuon.DataSource = tbl
        If (tbl.Rows.Count = 0) Then
            MessageBox.Show("không tìm thấy thông tin !")
        Else
        End If
        ob_cnn.Close()
    End Sub
    Private Sub cmbhuyen_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbhuyen.SelectedIndexChanged
        loadTenXa()
    End Sub
    Private Sub FrmHoSoDangMuon_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        NapDonViHuyen()
        NapTinhTrang()
        DateTuNgay.Text = DateAdd(DateInterval.Year, -1, Today())
        DateDenNgay.Text = Today()
        DataGridViewHoSoDangMuon.RowsDefaultCellStyle.BackColor = Color.Beige
        DataGridViewHoSoDangMuon.AlternatingRowsDefaultCellStyle.BackColor = Color.Azure
    End Sub
    Private Sub ButtonTimKiem_Click(sender As Object, e As EventArgs) Handles ButtonTimKiem.Click
        NapDataGridViewHoSoDangMuon()
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Me.Close()

    End Sub
    Private Sub ButtonXuatExcel_Click(sender As Object, e As EventArgs) Handles ButtonXuatExcel.Click
        XuatExcel()
    End Sub
    Private Sub DataGridViewHoSoDangMuon_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridViewHoSoDangMuon.RowPostPaint
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < DataGridViewHoSoDangMuon.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If DataGridViewHoSoDangMuon.RowHeadersWidth < Size.Width + 20 Then DataGridViewHoSoDangMuon.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))
    End Sub
End Class